//
//  progress.m
//  slider
//
//  Created by 邓芮 on 15/11/9.
//  Copyright © 2015年 邓芮. All rights reserved.
//

#import "progress.h"

@interface progress ()

@property (strong, nonatomic) UIView *cacheView;

@end

@implementation progress
{
    // 用于保存centerX的位置
    CGFloat centerX;
}

- (void)dealloc
{
    // 代理滞空
    _delegate = nil;
    // 移除KVO
    [self.thumb removeObserver:self forKeyPath:@"frame"];
    [self.slider removeObserver:self forKeyPath:@"value"];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        // 继承UIView 不继承UISlider 原因 UISlider内部添加的任何view都处于progress线下面 所以继承于UIView 创建的view添加在整个的slider上面
        // 一定要是self.bounds self.frame的话可能会造成视图偏差
        _slider = [[UISlider alloc]initWithFrame:self.bounds];
        
        // 设置slider图片 给一个空的图片
        [self.slider setThumbImage:[UIImage imageNamed:@"12-eye"] forState:UIControlStateNormal];
        
        // 添加一个target方法 获取value改变的属性 对thumb的位置进行修改
        [self.slider addTarget:self action:@selector(valueChange:) forControlEvents:UIControlEventValueChanged];
        
        [self addSubview:self.slider];
        
        
        // 缓冲条在slider上面 滑块下面
        self.cacheView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 2)];
        // 用户交互关闭
        self.cacheView.userInteractionEnabled = NO;
        [self addSubview:self.cacheView];
        
        
        // 添加KVO 用于改变了frame的大小 对center做一个修改
        [self.thumb addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
        
        // 创建滑块视图
        self.thumb = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 20, 20)];
        self.thumb.backgroundColor = [UIColor whiteColor];
        // 将用户交互关闭 防止响应链断裂 slider不可以滑动
        self.thumb.userInteractionEnabled = NO;
        self.thumb.center = CGPointMake(0, frame.size.height/2);
        
        self.thumb.layer.cornerRadius = 10;
        
        [self addSubview:self.thumb];
        
        // centerX初始值
        centerX = 0;
        
        
        
        // 添加tap 手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        //[self.slider addGestureRecognizer:tap];
        
        [self addGestureRecognizer:tap];
        
        // 添加一个KVO 观察slider的value值 然后对滑块位置做修改
        [self.slider addObserver:self forKeyPath:@"value" options:NSKeyValueObservingOptionNew context:nil];
        
        
    }
    return self;
}

#pragma mark -- KVO方法 --
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    if ([keyPath isEqualToString:@"frame"])
    {
        self.thumb.center = CGPointMake(centerX, self.frame.size.height / 2);
    }
    else if ([keyPath isEqualToString:@"value"])
    {
        // 修改滑块的位置
        CGFloat progress = self.slider.value / (self.slider.maximumValue - self.slider.minimumValue);
        self.thumb.center = CGPointMake(progress * self.frame.size.width, self.frame.size.height / 2);
    }
    
}

#pragma mark -- 轻拍手势 --
- (void)tapAction:(UITapGestureRecognizer *)tap
{
//    CGPoint point = [tap locationInView:tap.view];
//    self.slider.value = point.x / self.slider.frame.size.width * self.slider.maximumValue;
//    self.thumb.center = CGPointMake(point.x, self.frame.size.height / 2);
    
    // 获取tap手势边上的一个点
    CGPoint touch = [tap locationInView:self];
    // 范围判断
    if (touch.x < 0)
    {
        touch.x = 0;
    }
    else if (touch.x > self.frame.size.width)
    {
        touch.x = self.frame.size.width;
    }
    
    // 求出这个点所在的百分比
    CGFloat progress = touch.x / self.frame.size.width;
    // 根据百分点计算出value的值
    CGFloat value = (self.slider.maximumValue - self.slider.minimumValue) * progress;
    self.slider.value = value;
    // 更改thumb的位置
    self.thumb.center = CGPointMake(touch.x, self.frame.size.height / 2);
    // 传递参数
    if ([_delegate respondsToSelector:@selector(touchView:)])
    {
        [_delegate touchView:value];
    }
}

#pragma mark -- 滑块执行的方法 --
- (void)valueChange:(UISlider *)slider
{
    // 求出value的百分比
    CGFloat progress = slider.value / (slider.maximumValue - slider.minimumValue);
    // 求出thumbX轴的位置
    CGFloat thumbX = self.frame.size.width * progress;

    self.thumb.center = CGPointMake(thumbX, self.slider.frame.size.height / 2);
  
    // 保存centerX的值
    centerX = self.thumb.center.x;
}

#pragma mark -- 更换视图 重写set方法 --
- (void)setThumb:(UIView *)thumb
{
    // 先移除观察者
    [_thumb removeObserver:self forKeyPath:@"frame"];
    // 移除之前的视图
    [_thumb removeFromSuperview];
    // 添加新的视图
    [self addSubview:thumb];
    // 指针赋值
    _thumb = thumb;
    
    [_thumb addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
    // 让它走一次KVO方法
    _thumb.frame = thumb.frame;
}

#pragma mark -- 外部更改frame 内部子视图也作出相应的改变 --
- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    self.slider.frame = self.bounds;
}

#pragma mark -- 根据cacheValue进行赋值 --
- (void)setCache:(CGFloat)cache
{
    // 范围判断
    if (cache < self.slider.minimumValue)
    {
        cache = self.slider.minimumValue;
    }
    else if (cache > self.slider.maximumValue)
    {
        cache = self.slider.maximumValue;
    }
    
    // 求出百分比
    CGFloat progress = cache / (self.slider.maximumValue - self.slider.minimumValue);
    // 对cacheView的frame进行赋值
    self.cacheView.frame = CGRectMake(0, (self.frame.size.height - 2) / 2, progress * self.frame.size.width, 2);

}

#pragma mark -- 改变缓冲条的背景颜色 --
- (void)setCacheColor:(UIColor *)cacheColor
{
    self.cacheView.backgroundColor = cacheColor;
}

@end
